<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- pixelator.qdoc -->
  <title>Qt Quick TableViews examples - Pixelator | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >
			Qt Quick TableViews examples - 像素图</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">运行该示例</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick TableViews 示例 - 像素图</h1>
<span class="subtitle"></span>
<!-- $$$tableview/pixelator-brief -->
<p>Pixelator 示例展示了如何将 QML <a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-tableview.html">TableView</a>和委托用于自定义表格模型。</p>
<!-- @@@tableview/pixelator -->
<!-- $$$tableview/pixelator-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qt-pixelator.png" alt="" /></p><a name="running-the-example"></a>
<h2 id="running-the-example">运行该示例</h2>
<p>要从 <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>运行示例，请打开<b>欢迎</b> 模式并从 <b>示例</b>中选择示例。有关更多信息，请访问<a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">构建和运行示例</a>。</p>
<pre class="cpp">

  <span class="keyword">class</span> ImageModel : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qabstracttablemodel.html">QAbstractTableModel</a></span>
  {
      Q_OBJECT
      Q_PROPERTY(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> source READ source WRITE setSource NOTIFY sourceChanged)
  <span class="keyword">public</span>:
      ImageModel(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> source() <span class="keyword">const</span>;
      <span class="type">void</span> setSource(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>source);

      <span class="type">int</span> rowCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span>()) <span class="keyword">const</span> override;
      <span class="type">int</span> columnCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span>()) <span class="keyword">const</span> override;

      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> data(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index<span class="operator">,</span> <span class="type">int</span> role <span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole) <span class="keyword">const</span> override;

      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> headerData(<span class="type">int</span> <span class="comment">/* section */</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation <span class="comment">/* orientation */</span><span class="operator">,</span>
                          <span class="type">int</span> role) <span class="keyword">const</span> override;

  <span class="keyword">signals</span>:
      <span class="type">void</span> sourceChanged();

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> m_source;
      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> m_image;
  };

</pre>
<p>我们只需要一个简单的只读表模型。因此，我们需要实现函数来指示图像的尺寸并向<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-tableview.html">TableView</a>提供数据。我们使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/properties.html">Qt 属性系统</a>和源属性<code>QString</code>来设置图像的路径。</p>
<pre class="cpp">

  <span class="type">void</span> ImageModel<span class="operator">::</span>setSource(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>source)
  {
      <span class="keyword">if</span> (m_source <span class="operator">=</span><span class="operator">=</span> source)
          <span class="keyword">return</span>;

      beginResetModel();
      m_source <span class="operator">=</span> source;
      m_image<span class="operator">.</span>load(m_source);
      endResetModel();
  }

</pre>
<p>这里我们在设置源路径时加载图像。当源路径发生变化时，我们需要先调用<code>beginResetModel()</code>。图像加载完成后，我们需要调用<code>endResetModel()</code>.</p>
<pre class="cpp">

  <span class="type">int</span> ImageModel<span class="operator">::</span>rowCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (parent<span class="operator">.</span>isValid())
          <span class="keyword">return</span> <span class="number">0</span>;
      <span class="keyword">return</span> m_image<span class="operator">.</span>height();
  }

  <span class="type">int</span> ImageModel<span class="operator">::</span>columnCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (parent<span class="operator">.</span>isValid())
          <span class="keyword">return</span> <span class="number">0</span>;
      <span class="keyword">return</span> m_image<span class="operator">.</span>width();
  }

</pre>
<p>行数和列数分别设置为图像高度和宽度。</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> ImageModel<span class="operator">::</span>data(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index<span class="operator">,</span> <span class="type">int</span> role) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (<span class="operator">!</span>index<span class="operator">.</span>isValid() <span class="operator">|</span><span class="operator">|</span> role <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole)
          <span class="keyword">return</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>();
      <span class="keyword">return</span> <a href="../qtgui/qcolor.html#qGray-1">qGray</a>(m_image<span class="operator">.</span>pixel(index<span class="operator">.</span>column()<span class="operator">,</span> index<span class="operator">.</span>row()));
  }

</pre>
<p>这个重载函数允许我们访问图像中的像素数据。当我们用显示角色调用这个函数时，我们返回像素的灰度值。</p>
<pre class="cpp">

  qmlRegisterType<span class="operator">&lt;</span>ImageModel<span class="operator">&gt;</span>(<span class="string">&quot;ImageModel&quot;</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;ImageModel&quot;</span>);

</pre>
<p>我们需要在 QML 类型系统中注册我们的模型，以便能够从 QML 端使用它。</p>
<pre class="qml">

  <span class="type"><a href="../qtqml/qml-qtqml-component.html">Component</a></span> {
      <span class="name">id</span>: <span class="name">pixelDelegate</span>

      <span class="type"><a href="qml-qtquick-item.html">Item</a></span> {
          readonly property <span class="type"><a href="../qtqml/qml-real.html">real</a></span> <span class="name">gray</span>: <span class="name">model</span>.<span class="name">display</span> <span class="operator">/</span> <span class="number">255.0</span>
          readonly property <span class="type"><a href="../qtqml/qml-real.html">real</a></span> <span class="name">size</span>: <span class="number">16</span>

          <span class="name">implicitWidth</span>: <span class="name">size</span>
          <span class="name">implicitHeight</span>: <span class="name">size</span>

</pre>
<p><code>TableView</code> 
中的每个像素都是通过一个委托组件来显示的。它包含一个具有定义表的单元格大小的隐式高度和宽度的项。它还有一个属性，用于从模型中检索到的像素的灰度值。</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">rect</span>
      <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
      <span class="name">color</span>: <span class="string">&quot;#09102b&quot;</span>
      <span class="name">radius</span>: <span class="name">size</span> <span class="operator">-</span> <span class="name">gray</span> <span class="operator">*</span> <span class="name">size</span>
      <span class="name">implicitWidth</span>: <span class="name">radius</span>
      <span class="name">implicitHeight</span>: <span class="name">radius</span>

</pre>
<p>在<code>Item</code>中，有一个圆角<code>Rectangle</code> ，其大小和半径根据像素的灰度值。</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> {
      <span class="name">anchors</span>.fill: <span class="name">parent</span>
      <span class="name">hoverEnabled</span>: <span class="number">true</span>
      <span class="name">onEntered</span>: <span class="name">rect</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">&quot;#cecfd5&quot;</span>
      <span class="name">onExited</span>: <span class="name">colorAnimation</span>.<span class="name">start</span>()
  }

</pre>
<p>了进行一些交互，我们在Item中放置一个<code>MouseArea</code> ，并更改鼠标上方的矩形颜色。</p>
<pre class="qml">

  ColorAnimation on <span class="name">color</span> {
      <span class="name">id</span>: <span class="name">colorAnimation</span>
      <span class="name">running</span>: <span class="number">false</span>
      <span class="name">to</span>: <span class="string">&quot;#41cd52&quot;</span>
      <span class="name">duration</span>: <span class="number">1500</span>
  }

</pre>
<p>也有一个简短的<code>Rectangle</code>颜色动画，当它改变时在颜色之间淡出。</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-tableview.html">TableView</a></span> {
      <span class="name">id</span>: <span class="name">tableView</span>
      <span class="name">anchors</span>.fill: <span class="name">parent</span>
      <span class="name">model</span>: <span class="name">ImageModel</span> {
          <span class="name">source</span>: <span class="string">&quot;:/qt.png&quot;</span>
      }

      <span class="name">delegate</span>: <span class="name">pixelDelegate</span>
  }

</pre>
<p><code>TableView</code>跨越整个窗口，并附有我们自定义的实例<code>ImageModel</code>。</p>
<p>文件：</p>
<ul>
<li><a href="qtquick-tableview-pixelator-imagemodel-cpp.html">tableview/pixelator/imagemodel.cpp</a></li>
<li><a href="qtquick-tableview-pixelator-imagemodel-h.html">tableview/pixelator/imagemodel.h</a></li>
<li><a href="qtquick-tableview-pixelator-main-cpp.html">tableview/pixelator/main.cpp</a></li>
<li><a href="qtquick-tableview-pixelator-main-qml.html">tableview/pixelator/main.qml</a></li>
<li><a href="qtquick-tableview-pixelator-pixelator-pro.html">tableview/pixelator/pixelator.pro</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/tableview/pixelator/qt.png">tableview/pixelator/qt.png</a></li>
</ul>
</div>
<!-- @@@tableview/pixelator -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
